<template>
    <div class="content">
        <div class="common-seach-wrap" :style="{ marginBottom: '10px' }">
            <el-button type="primary" icon="Plus" @click="handleAdd" v-auth="'ducument_add'">添加合同</el-button>
        </div>
        <el-table :data="tableData" row-key="id" height="100%" stripe>
            <el-table-column type="index" label="#" fixed="left" />
            <el-table-column prop="title" label="标题" fixed="left" />
            <el-table-column prop="user.real_name" label="姓名" fixed="left" />
            <el-table-column sortable label="状态">
                <template #default="scope">
                    <el-tag type="warning" v-if="scope.row.status == 0">待签</el-tag>
                    <el-tag type="success" v-else-if="scope.row.status == 1">已签</el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="create_time" sortable label="创建时间" />
            <el-table-column prop="update_time" sortable label="最后修改" />
            <!-- <el-table-column prop="sort" sortable label="排序" /> -->
            <el-table-column label="操作" fixed="right" width="200">
                <template #default="scope">
                    <el-button type="primary" link icon="Edit" @click="handleOpen(scope.row.id)">
                        详情
                    </el-button>
                    <el-button type="danger" link icon="Delete" @click="del(scope.row.id)" v-auth="'contract_del'">
                        作废
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-divider border-style="none" />
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" background
            :current-page="curPage" :page-size="pageSize" layout="total, sizes,prev, pager, next, jumper"
            :total="total"></el-pagination>
        <Drawer ref="detailDraw" />
        <AddDrawer ref="addDraw" @getTable="getTableInfo" />
    </div>
</template>

<script setup lang="ts">
import { ref, onMounted, defineEmits, reactive, watch } from "vue";
import Drawer from "./component/detailDialog.vue";
import AddDrawer from "./component/addDialog.vue";
import { ElMessage, ElMessageBox, TabPaneName } from "element-plus";
import { delApi, getListAPi } from "@/api/contract/contract";
const detailDraw = ref<any>(null);
const addDraw = ref<any>(null);
const query = reactive({
    
});
const total = ref(0);
watch(
    query,
    () => {
        getTableInfo();
    },
    { deep: true }
);
const curPage = ref(1);
const pageSize = ref(20);
const tableData = ref([]);
onMounted(() => {
    getTableInfo();
});
const handleSizeChange = (val: number) => {
    pageSize.value = val;
    getTableInfo();
};
const handleCurrentChange = (val: number) => {
    curPage.value = val;
    getTableInfo();
};
function getTableInfo() {
    getListAPi({
        page: curPage.value,
        page_size: pageSize.value,
        ...query,
    }).then((res) => {
        tableData.value = res.data.data;
        total.value = res.data.total;
    });
}
function handleOpen(id: number) {
    detailDraw.value.dialogOpen(id);
}

function handleAdd() {
    addDraw.value.dialogOpen();
}
function del(id:number){
    ElMessageBox.confirm("确定删除该合同？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
    })
    .then(() => {
        delApi(id).then((res) => {
            if (res.code == 1) {
                ElMessage.success("删除成功");
                getTableInfo();
            }else {}
        })
    })
}
defineExpose({
    getTableInfo,
});
</script>

<style lang="scss">
.el-tabs {
    .el-tabs__new-tab {
        width: auto !important;
    }
}

.table-box {
    height: calc(100% - 200px);
}
</style>